using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace ClientTools.Client.Settings
{
    public class Profile
    {
        [XmlAttribute]
        public string ProfileName;
        public string UmbracoUserName;
        public string UmbracoPassword;
        public string DBConnectionString;
        public string MaintenanceServiceURL;

        private string _ProviderName;

        public string ProviderName
        {
            get { return _ProviderName; }
            set { _ProviderName = value; _ProviderObject = null; }
        }


        public void InitProvider() 
        {
            Library.Provider.ProviderInfo pi = Library.Provider.ProviderManager.ProviderInfos.GetProviderByName(_ProviderName);
            if (pi != null)
            {
                _ProviderObject = (Library.Interfaces.I_Provider)Activator.CreateInstance(pi.ProviderType);
            }
            switch (_ProviderObject.GetType().Name)
            {
                case "WebserviceProvider":
                    Library.Provider.WebserviceProvider wsp = (Library.Provider.WebserviceProvider) _ProviderObject;
                    wsp.MaintenanceServiceUrl = this.MaintenanceServiceURL;
                    wsp.UserName = this.UmbracoUserName;
                    wsp.Password = this.UmbracoPassword;
                    break;
                case "DatabaseProvider":
                    Library.Provider.DatabaseProvider dbp = (Library.Provider.DatabaseProvider) _ProviderObject;
                    dbp.ConnectionString = this.DBConnectionString;
                    break;
            }
        }

        private Library.Interfaces.I_Provider _ProviderObject;
        [XmlIgnore] 
        public Library.Interfaces.I_Provider ProviderObject 
        {
            get {
                if (_ProviderObject == null)
                {
                    Library.Provider.ProviderInfo pi = Library.Provider.ProviderManager.ProviderInfos.GetProviderByName(_ProviderName);
                    if (pi != null)
                    {
                        _ProviderObject = (Library.Interfaces.I_Provider)Activator.CreateInstance(pi.ProviderType);
                    }
                }
                return _ProviderObject; 
            }
            set { 
                _ProviderObject = value;
                _ProviderName = _ProviderObject.Providername;
            }
        }
    }
}
